package com.cai.quartz.schedule;

import com.cai.quartz.job.MyJob3;
import com.cai.quartz.trigger.MyTrigger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

//@Configuration
public class MySchedule4 {
    public MySchedule4() throws SchedulerException {
        doSomeThing();
    }

    public void doSomeThing() throws SchedulerException {
        //1.调度器 从工厂中获取调度的实例
        Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();

        //2.任务实例 JobDetail
        JobDetail jobDetail = JobBuilder
                .newJob(MyTrigger.class)
                // 任务名称  任务组名称
                .withIdentity("job")
                .usingJobData("count",0)
                .build();

        //3.触发器
        Date statrtDate = DateBuilder.futureDate(3, DateBuilder.IntervalUnit.SECOND);
        Date endDate = DateBuilder.futureDate(13, DateBuilder.IntervalUnit.SECOND);
        Trigger trigger = TriggerBuilder
                .newTrigger()
                // 触发器名称  触发器组组名称
                .withIdentity("job1")
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
                //马上执行
                //.startNow()
                //设置任务开始时间 延迟三秒
                .startAt(statrtDate)
                //设置任务结束时间 延迟十秒
                .endAt(endDate)
                .build();

        //调度器管理任务和触发器，保证按照触发器定义的条件执行任务
        scheduler.scheduleJob(jobDetail,trigger);
        //启动
        scheduler.start();
    }
}
